說到Key / Value 大家第一想到的應該也跟我一樣 "etcd" 或是 "ZooKeeper", 官網也有一篇文章介紹,google也有不少比較表,這邊就不再英翻中或是換句話說了。
今天會介紹Consul kv的使用方式,包含get, put, import ,export 以及 delete.
目的: 將需要動態配置的資料存放於此,透過Consul 提供HTTP API的呼叫來達到即時性。
Value: Value的建議最大值在512kb, 可以透過參數kv_max_value_size來調整,文件有備註說明,不適當的值可能會影響效能或是發生問題。
Key: Key 的命名原則上不受限制,但官方建議使用URL的安全字元[a-zA-Z0-9-_]並排除"/", 也避免使用*, ?, ', % 容易在http api與shell scripts上出錯。 "/" 會被使用在區分目錄結構上。
Note:
若無特別的配置,在跨DC的K/V是獨立於各數據中心的,但可以透過consul-replicate tool來定期複製資料。
假設要設定ERP系統的http session參數,可以這樣子設定
$ consul kv put erp/http/minsession 1
Success! Data written to: erp/http/minsession
consul kv put erp/http/maxsession 50
Success! Data written to: erp/http/maxsession
$ consul kv get erp/http/minsession
1
$ consul kv get erp/http/maxsession
50
$ consul kv delete erp/http/minsession
Success! Deleted key: erp/http/minsession
要修改資料與新增相同,Consul會直接覆蓋舊的資料。
$ consul kv put erp/http/maxsession 100
Success! Data written to: erp/http/maxsession
$ consul kv get -recurse
erp/http/maxsession:100
erp/http/minsession:1
ithome/http/maxsession:123
ithome/http/minsession:20
order/http/maxsession:100
order/http/minsession:10
例如列出order開頭的
$ consul kv get -recurse order/
order/http/maxsession:100
order/http/minsession:10
這邊會看到除了key, vale外,還會有flag, 因為在寫入時沒有指定flag,所以都是0.
Value的值怎麼怪怪的,這是被base64編碼過了,需要再解密人類才能看得懂。
$ consul kv export
[
{
"key": "erp/http/maxsession",
"flags": 0,
"value": "MTAw"
},
{
"key": "erp/http/minsession",
"flags": 0,
"value": "MQ=="
},
{
"key": "ithome/http/maxsession",
"flags": 0,
"value": "MTIz"
},
{
"key": "ithome/http/minsession",
"flags": 0,
"value": "MjA="
},
{
"key": "order/http/maxsession",
"flags": 0,
"value": "MTAw"
},
{
"key": "order/http/minsession",
"flags": 0,
"value": "MTA="
}
]
base64 解密,以order/http/minsession為例。
$ base64 --decode <<< "MTA="
10
$ consul kv export order/
[
{
"key": "order/http/maxsession",
"flags": 0,
"value": "MTAw"
},
{
"key": "order/http/minsession",
"flags": 0,
"value": "MTA="
}
]
匯入的話也需先將value加密為base 64,先將兩個值加密
$ base64 <<< "66"
NjYK
$base64 <<< "88"
ODgK
編輯要匯入的json file
# pay.json
[
{
"key": "pay/http/maxsession",
"flags": 0,
"value": "ODgK"
},
{
"key": "pay/http/minsession",
"flags": 0,
"value": "NjYK"
}
]
執行匯入
$ consul kv import @pay.json
Imported: pay/http/maxsession
Imported: pay/http/minsession
查詢看看是否成功
$ consul kv get pay/http/maxsession
88
$ consul kv get pay/http/minsession
66